{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "559a36b4-0958-479e-97da-4bb7c8f15d24",
   "metadata": {},
   "source": [
    "# Pandas数学函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23562bb5-0965-4241-97f7-d3ec2ac2a99f",
   "metadata": {},
   "source": [
    "聚合函数：\n",
    "- count():非空值的数量\n",
    "- max():最大值\n",
    "- min():最小值\n",
    "- median():中位数\n",
    "- sum():求和\n",
    "- mean():每一行的平均数\n",
    "\n",
    "其他函数\n",
    "- value_counts():统计元素出现次数\n",
    "- cumsum():累加\n",
    "- cumprod():累乘\n",
    "- std():标准差\n",
    "- var():方差\n",
    "- cov():协方差\n",
    "- df.corr():所有属性相关性系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "35d20bd0-ce30-4cb1-a780-d52d90231458",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa5e34ea-07ae-4184-beb5-70de46d76859",
   "metadata": {},
   "source": [
    "- 聚合函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4f236b3c-fe08-4a5d-9132-92a9264bb6ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>82</td>\n",
       "      <td>82</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>16</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>48</td>\n",
       "      <td>43</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2\n",
       "0  82  82  49\n",
       "1  14  16  11\n",
       "2  48  43  48\n",
       "3  44  97  97\n",
       "4   9   9  52"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data=np.random.randint(0,100,size=(5,3)))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4a62f0af-a3aa-4bb6-8d29-4af4e2c484cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    71.000000\n",
       "1    13.666667\n",
       "2    46.333333\n",
       "3    79.333333\n",
       "4    23.333333\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.count()# 默认求每一列非空的数量\n",
    "df.count(axis=1)#求每一行非空的数量\n",
    "\n",
    "\n",
    "df.max()# 默认求每一列不同行的最大值\n",
    "df.max(axis=1)# 求每一行不同列的最大值\n",
    "\n",
    "df.min()# 同上\n",
    "df.min(axis=1)# 同上\n",
    "\n",
    "df.median()# 默认每一列，中位数\n",
    "\n",
    "df.sum()# 默认每一列，求和\n",
    "df.values.sum()# 求所有数的和\n",
    "df.sum(axis=1)\n",
    "\n",
    "df.mean()# 平均值\n",
    "df.mean(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaa9675f-818b-40d9-a0ae-b852617844cd",
   "metadata": {},
   "source": [
    "- 方差：\n",
    "- - 当前数据分布比较分散（即数据在平均数附近波动较大）时，各个数据与平均数的差的平方和较大，方差就较大；\n",
    "  - 当数据分布比较集中时，各个数据与平均数的差的平方和较小。\n",
    "  - 因此方差越大，数据波动就越大；方差越小数据的波动就越小\n",
    "- 标准差\n",
    "- - 标准差=方差的算术平方根"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "62abf920-04a5-4a2b-a500-1cf9330d4796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>82</td>\n",
       "      <td>82</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>16</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>48</td>\n",
       "      <td>43</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2\n",
       "0  82  82  49\n",
       "1  14  16  11\n",
       "2  48  43  48\n",
       "3  44  97  97\n",
       "4   9   9  52"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d5274f93-2e1e-43f8-a923-e4965708ac97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     869.8\n",
       "1    1529.3\n",
       "2     932.3\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.var() # 每一列方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "420369cd-42e8-467d-9e77-a7e6967eb993",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    29.492372\n",
       "1    39.106265\n",
       "2    30.533588\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.std() # 每一列标准差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a06ff37-3c22-4fb1-9187-916dd4a63c72",
   "metadata": {},
   "source": [
    "- 其他数学函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f3349622-9c7b-4978-a3fe-6a3558044ec7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1\n",
       "82    1\n",
       "16    1\n",
       "43    1\n",
       "97    1\n",
       "9     1\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计元素出现次数\n",
    "df[1].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f664ad6e-45f9-444b-a7ec-742de2c0dc28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>82</td>\n",
       "      <td>82</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>96</td>\n",
       "      <td>98</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>144</td>\n",
       "      <td>141</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>188</td>\n",
       "      <td>238</td>\n",
       "      <td>205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>197</td>\n",
       "      <td>247</td>\n",
       "      <td>257</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0    1    2\n",
       "0   82   82   49\n",
       "1   96   98   60\n",
       "2  144  141  108\n",
       "3  188  238  205\n",
       "4  197  247  257"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumsum()#累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "67369b8c-0632-495e-9424-a92da470e4aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>82</td>\n",
       "      <td>82</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1148</td>\n",
       "      <td>1312</td>\n",
       "      <td>539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>55104</td>\n",
       "      <td>56416</td>\n",
       "      <td>25872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2424576</td>\n",
       "      <td>5472352</td>\n",
       "      <td>2509584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>21821184</td>\n",
       "      <td>49251168</td>\n",
       "      <td>130498368</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1          2\n",
       "0        82        82         49\n",
       "1      1148      1312        539\n",
       "2     55104     56416      25872\n",
       "3   2424576   5472352    2509584\n",
       "4  21821184  49251168  130498368"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumprod()# 累乘"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eecbdc6-f67f-4237-8ae7-4b235e9a33b5",
   "metadata": {},
   "source": [
    "- 协方差\n",
    "  - 两组数值中每对变量的偏差乘积的平均值\n",
    "  - 协方差>0:表示两组数据正相关\n",
    "    - 如果两个变量的变化趋势一致，也就是说如果其中一个大于自身的期望值另一个也大于自身的期望值，那么两个变量之间的协方差就是正值；\n",
    "   \n",
    "  - 协方差<0:表示两组变量负相关\n",
    "    - 如果两个变量的变化趋势相反，即其中一个变量大于自身的期望值时另外一个却小于自身的期望值，那么两个变量之间的协方差就是负值。\n",
    "   \n",
    "  - 协方差=0：表示两组变量不相关"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "639ca359-b5a8-426e-b13c-89fc1337abb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(907.3000000000001)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cov() # 默认求所有协方差\n",
    "df[0].cov(df[1])# 第0列和第1列协方差"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1132697-8393-405b-8488-a9cec50667ec",
   "metadata": {},
   "source": [
    "- 相关系数r\n",
    "  - 相关系数 = X与Y的协方差 / (X的标准差 * Y的标准差)\n",
    "  - 相关系数值的范围在-1和+1之间\n",
    "  - r>0为正相关,r<0为负相关,r=0为不相关\n",
    "  - r的绝对值越大，相关程度越高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e62514af-def5-4981-9c6c-f25afd74b764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.786674</td>\n",
       "      <td>0.301552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.786674</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.720068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.301552</td>\n",
       "      <td>0.720068</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2\n",
       "0  1.000000  0.786674  0.301552\n",
       "1  0.786674  1.000000  0.720068\n",
       "2  0.301552  0.720068  1.000000"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr() # 默认求所有特征相关系数\n",
    "# df.corrwith(df[2])# 单一特征相关系数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
